package
{
	import away3d.cameras.HoverCamera3D;
	import away3d.containers.Scene3D;
	import away3d.containers.View3D;
	import away3d.core.render.Renderer;
	import away3d.lights.DirectionalLight3D;
	import away3d.lights.PointLight3D;
	import away3d.materials.BitmapMaterial;
	import away3d.materials.ColorMaterial;
	import away3d.materials.PhongColorMaterial;
	import away3d.materials.utils.data.RenderData;
	import away3d.primitives.Cube;
	import away3d.primitives.Plane;
	import away3d.primitives.Sphere;
	
	import flash.display.FrameLabel;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.GestureEvent;
	import flash.events.MouseEvent;
	import flash.events.TouchEvent;
	import flash.events.TransformGestureEvent;
	import flash.geom.Point;
	import flash.geom.Vector3D;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.text.TextField;
	import flash.utils.setInterval;
	
	[SWF(width="500", height="300", frameRate="60", backgroundColor="#000000")]
	public class Molehill extends Sprite
	{
		private var scene:Scene3D;
		private var view:View3D;
		private var camera:HoverCamera3D;
		private var cube:Cube;
		private var sphere:Sphere;
		private var material:PhongColorMaterial;
		private var light:DirectionalLight3D;
		private var groundMaterial:BitmapMaterial;
		private var ground:Plane;
		private var sound:Sound;
		private var soundChannel:SoundChannel;
		private var isSoundPlaying:Boolean;
		
		private var text:TextField;
		private var fps:int = 0;
		
		
		private var isMouseDown:Boolean;
		private var mouseDownPoint:Point;
		private var lastPanAngle:Number;
		private var lastTiltAngle:Number;
		
		[Embed(source="embeds/CoarseRedSand.jpg")]
		private var SandTexture:Class;
		
		[Embed(source="embeds/Terminator_3_theme_HQ.mp3")]
		private var Terminator:Class;
		
		public function Molehill()
		{
			setInterval(showFPS, 1000);
			sound = new Terminator();
			soundChannel = sound.play();
			isSoundPlaying = true;
			addEventListener(GestureEvent.GESTURE_TWO_FINGER_TAP, soundControl);
			addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
		}
		
		protected function soundControl(event:GestureEvent):void
		{
			if(isSoundPlaying){
				soundChannel.stop();
				isSoundPlaying = false;
			}else{
				soundChannel = sound.play();
				isSoundPlaying = true;
			}
		}
		
		private function showFPS():void
		{
			text.text = fps + " FPS";
			fps = 0;
		}
		
		protected function addedToStageHandler(event:Event):void
		{
			text = new TextField();
			text.textColor = 0xFFFFFF;
			
			camera = new HoverCamera3D();
			camera.zoom = 4;
			camera.distance = 900;
			camera.minTiltAngle = 10;
			camera.panAngle = 0;
			camera.tiltAngle = 35;
			camera.hover(true);
			
			scene = new Scene3D();
			
			view = new View3D();
			view.x = stage.fullScreenWidth / 2;
			view.y = stage.fullScreenHeight / 2;
			view.scene = scene;
			view.camera = camera;
			addChild(view);
			addChild(text);
			
			
			material = new PhongColorMaterial(0xff0000);

			
			light = new DirectionalLight3D();
			light.color = 0xFFFFFF;
			light.ambient = 0;
			light.diffuse = 0.5;
			light.specular = 1;
			light.brightness = 1;
			light.debug = true;
			
			
			groundMaterial = new BitmapMaterial((new SandTexture()).bitmapData);
			ground = new Plane({segmentsW:1, segmentsH:1, y:-120, width:1100, height:1100});
			ground.material = groundMaterial;
			ground.bothsides = true;
			scene.addChild(ground);
			
			cube = createCube(200, 200, 200, material);
			cube.x = 200;
			cube.y = 100;
			cube.z = 300;
			
			sphere = createSphere(100, material);
			sphere.x = -200;
			sphere.y = 100;
			sphere.z = -300;
			
			
			scene.addChild(cube);
			scene.addChild(sphere);
			scene.addLight(light);
			
			addEventListener(Event.ENTER_FRAME, enterFrameHandler);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
			
			addEventListener(TransformGestureEvent.GESTURE_ZOOM, zoomHandler);
		}
		
		protected function zoomHandler(event:TransformGestureEvent):void
		{
//			trace(event.scaleX);
			if(event.scaleX < 1)
				camera.zoom -= 1;
			else
				camera.zoom += 1;
		}
		
		protected function mouseUpHandler(event:MouseEvent):void
		{
			isMouseDown = false;	
			mouseDownPoint = null;
		}
		
		protected function mouseDownHandler(event:MouseEvent):void
		{
			isMouseDown = true;
			lastPanAngle = camera.panAngle;
			lastTiltAngle = camera.tiltAngle;
			mouseDownPoint = new Point(event.stageX, event.stageY);
		}
		
		protected function enterFrameHandler(event:Event):void
		{
			fps++;
			cube.rotationY += 1;
			if (isMouseDown) {
				camera.panAngle = 0.3 * (stage.mouseX - mouseDownPoint.x) + lastPanAngle;
				camera.tiltAngle = 0.3 * (stage.mouseY - mouseDownPoint.y) + lastTiltAngle;
			}
			camera.hover();
			view.render();			
		}
		
		protected function createCube(w:Number, h:Number, d:Number, m:PhongColorMaterial):Cube
		{
			var cube:Cube = new Cube();
			cube.width = w;
			cube.height = h;
			cube.depth = d;
			cube.material = m;
			return cube;
		}
		
		protected function createSphere(r:Number, m:PhongColorMaterial):Sphere
		{
			var sphere:Sphere = new Sphere();
			sphere.radius = r;
			sphere.material = m;
			return sphere;
		}
	}
}